//
//  ViewController.m
//  25-多线程
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    进程和线程
//    ios进程
//    进程是当用户下达执行应用程序的命令时，所启动的东西
//    线程 是进程的执行单位 线程比作工人，进程是工厂
//    多线程:为了更好的进行一些程序的操作，节省时间提高效率
//    一个进程不能创造太多线程，资源消耗过大，开辟5个左右最合适
//    注意：所有UI的操作都放在主线程进行
//    需要开辟线程的耗时的操作，图像文字渲染，大数据存储，网络请求
    
//    1.NSThread
//    当前线程
//    name线程名 number线程编号
//    main代表主线程，
    NSThread *thread = [NSThread currentThread];
//    NSLog(@"%@",[NSThread currentThread]);
//    2.开辟子线程执行操作
//    子线程随机
//    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
//    NSLog(@"%@",thread.threadDictionary);
//    3延迟当前线程执行,固定日期
//    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
//    NSLog(@"11111");
    
//    4.相对时间
//    [NSThread sleepForTimeInterval:5];
//    NSLog(@"1111111");
//    5退出当前线程,不要退出主线程，退出后线程后续操作不会在执行
//    [NSThread exit];
    
//    NSLog(@"%@",[NSThread mainThread]);
//    6默认线程优先级都是0.5
    double priority = [NSThread threadPriority];
//    NSLog(@"%lf",priority);
//    7线程名
//    thread.name = @"xxx的线程";
//    NSLog(@"%@",thread.name);
//    thread.isMainThread
//    NSLog(@"%d",[thread.isMainThread);
//    8判断当前线程是否是主线程
//    [NSThread isMainThread];
//    判断线程对象是不是主线程
//    [thread isMainThread];
//    9.初始化一个未启动的线程,需要手动管理线程的生命，
//    NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImage) object:nil];
//    [newThread start];
    
    
//    练习
//    [self loadImage];
//    10.将耗时的操作放入后台不关心线程问题
    [self performSelectorInBackground:@selector(loadImage) withObject:nil];
//    11.返回主线程执行操作performSelectorOnMainThread
}

- (void)loadImage
{
    NSURL *imageURL = [NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"];
//    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
//    imageURLRequest.HTTPMethod = @"GET";
//    NSURLResponse *response = nil;
    NSData *data = [NSData dataWithContentsOfURL:imageURL];
//    返回主线程，显示图片
    [self performSelectorOnMainThread:@selector(onMainT:) withObject:data waitUntilDone:YES];
//    NSLog(@"%@",data);
}

- (void)onMainT:(id)object
{
    _imageView.image = [UIImage imageWithData:object];
}


- (void)myThread
{
    
    
// NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThread) object:nil];
    for (int i = 0; i < 100; i++) {
        if (i == 99) {
            NSLog(@"%d",i);
            [NSThread exit];
        }
    }
    
 NSLog(@"%s",__FUNCTION__);
}

- (void)newThread
{
    NSThread *thread = [NSThread currentThread];
    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
//    NSLog(@"11111");
//    NSLog(@"%@",thread);
    double priority = [NSThread threadPriority];
    NSLog(@"%lf",priority);
}

- (IBAction)clicked:(id)sender {
    
    for (int i = 0; i < 100000000; i++) {
        NSLog(@"循环了%d次",i);
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
